package com.open.party.cloud.view.login

import android.os.Bundle
import android.view.View
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import cn.android.x.utils.ImmSolftManager
import com.open.party.cloud.R
import com.open.party.cloud.model.UserVo
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.viewModel.BaseViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_pwd_change.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 */
class PwdChangeActivity : AppEventBusTitleBaseActivity(), View.OnClickListener {

    private var viewModel: BaseViewModel? = null

    override fun getLayoutResId(): Int = R.layout.activity_pwd_change

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("密码修改")

        viewModel = BaseViewModel()

        initView()
        initListener()
    }

    private fun initView() {
    }

    private fun initListener() {
        submitBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        ImmSolftManager(this).showKeyboard(false)
        when (v) {
            submitBtn -> {
                submit()
            }
        }
    }

    private fun submit() {
        if (!XUtils.net().isConnected) {
            XUtils.toast().show("当前网络不可用")
            return
        }

        val userVo = UserVo()

        val oldPWd = oldPWdEt.text.toString()
        if (XUtils.string().isEmpty(oldPWd)) {
            XUtils.toast().show("请输入原密码")
            return
        } else {
            userVo.oldPassword = XUtils.codeByMd5().stringToMD5(oldPWd)
        }
        val newPWd = newPWdEt.text.toString()
        if (XUtils.string().isEmpty(newPWd)) {
            XUtils.toast().show("请输入新密码")
            return
        } else if (newPWd.length > 12 || newPWd.length < 6) {
            XUtils.toast().show("密码长度应在6-18个字")
            return
        }

        val new2PWd = new2PWdEt.text.toString()
        if (XUtils.string().isEmpty(new2PWd)) {
            XUtils.toast().show("请再次输入新密码")
            return
        }

        if (newPWd != new2PWd) {
            XUtils.toast().show("两次输入的新密码不一致")
            return
        }

        userVo.newPassword = XUtils.codeByMd5().stringToMD5(newPWd)

        showLoadingDialog("正在更新")
        viewModel?.updatePassword(userVo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<String>) {

        if ("updatePassword" != result.eventType) {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("密码修改成功")
                LocalCache.logout()
                XUtils.page().finishAll()
                XUtils.intent().openActivity(this@PwdChangeActivity, WelcomeActivity::class.java).start()
            }
            ResultInfo.TOKEN_OVERDUE -> {
                // 不存在
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}